package com.gavin.sample.security;

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

public class RSAUtil {
	public static final String RSA = "RSA";
	
	public static KeyPair getKeyPair() throws Exception {
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA);
		keyPairGenerator.initialize(512);
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		return keyPair;
	}
	
	public static String getPublicKey(KeyPair keyPair) {
		PublicKey publicKey = keyPair.getPublic();
		byte[] bytes = publicKey.getEncoded();
		return Base64.encodeBase64String(bytes);
	}
	
	public static String getPrivateKey(KeyPair keyPair) {
		PrivateKey privateKey = keyPair.getPrivate();
		byte[] bytes = privateKey.getEncoded();
		return Base64.encodeBase64String(bytes);
	}
	
	public static PublicKey string2PublicKey(String publicStr) throws Exception {
		byte[] keyBytes = Base64.decodeBase64(publicStr);
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(RSA);
		PublicKey publicKey = keyFactory.generatePublic(keySpec);
		return publicKey;
	}
	
	public static PrivateKey string2PrivateKey(String privateStr) throws Exception {
		byte[] keyBytes = Base64.decodeBase64(privateStr);
		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance(RSA);
		PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
		return privateKey;
	}
	
	public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception {
		Cipher cipher = Cipher.getInstance(RSA);
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		byte[] bytes = cipher.doFinal(content);
		return bytes;
	}
	
	public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception {
		Cipher cipher = Cipher.getInstance(RSA);
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] bytes = cipher.doFinal(content);
		return bytes;
	}
	
	public static void main(String[] args) throws Exception {
		KeyPair keyPair = getKeyPair();
		String publicKey = getPublicKey(keyPair);
		String privateKey = getPrivateKey(keyPair);
		System.out.println("public key:" + publicKey);
		System.out.println("private key:" + privateKey);
		
		String str = "hi,i'm gavin";
		System.out.println("the string is " + str);
		byte[] encryptBytes = publicEncrypt(str.getBytes("UTF-8"), string2PublicKey(publicKey));
		String encryptStr = Base64.encodeBase64String(encryptBytes);
		System.out.println("the string after encrypt by RSA is " + encryptStr);
		
		byte[] decryptBytes = privateDecrypt(Base64.decodeBase64(encryptStr), string2PrivateKey(privateKey));
		String decryptStr = new String(decryptBytes, "UTF-8");
		System.out.println("the string after decrypt by RSA is " + decryptStr);
	}
}
